#include "noteflow.h"
#include <QPainter>
#include "../shape/note.h"
#include "flowtext.h"
#include "tempshape.h"
#include "../shape/action.h"
#include "../shape/decision.h"
#include "../shape/initialnode.h"
#include "../shape/finalnode.h"

NoteFlow *NoteFlow::clone() const
{
	NoteFlow *newflow = new NoteFlow(startShape(), endShape());
	newflow->knots = this->knots;
	newflow->updateAnchors();
	return newflow;
}

bool NoteFlow::canConnect (Shape *s, Shape *e)
{
	if (dynamic_cast<TempShape *>(s) || dynamic_cast<TempShape *>(e)) return false;
	if (s==e) return false;
	if (dynamic_cast<FlowText *>(s) || dynamic_cast<FlowText *>(e)) return false;
	if (!(
			(bool)dynamic_cast<NoteShape *>(s) ^ (bool)dynamic_cast<NoteShape *>(e)
		))
		return false;
	if (dynamic_cast<NoteFlow *>(s) || dynamic_cast<NoteFlow *>(e)) return false;
	return true;
}

bool NoteFlow::canConnectStartTo (Shape *s)
{
	if (dynamic_cast<NoteShape *>(endShape()))
		return dynamic_cast<ActionShape *>(s)
			|| dynamic_cast<DecisionShape *>(s)
			|| dynamic_cast<InitialNodeShape *>(s)
			|| dynamic_cast<FinalNodeShape *>(s)
			|| dynamic_cast<ArrowFlow *>(s);
	else if (dynamic_cast<TempShape *>(endShape()))
		return dynamic_cast<ActionShape *>(s)
			|| dynamic_cast<DecisionShape *>(s)
			|| dynamic_cast<InitialNodeShape *>(s)
			|| dynamic_cast<FinalNodeShape *>(s)
			|| dynamic_cast<ArrowFlow *>(s)
			|| dynamic_cast<NoteShape *>(s);
	else return dynamic_cast<NoteShape *>(s);
}

bool NoteFlow::canConnectEndTo (Shape *s)
{
	if (dynamic_cast<NoteShape *>(startShape()))
		return dynamic_cast<ActionShape *>(s)
			|| dynamic_cast<DecisionShape *>(s)
			|| dynamic_cast<InitialNodeShape *>(s)
			|| dynamic_cast<FinalNodeShape *>(s)
			|| dynamic_cast<ArrowFlow *>(s);
	else if (dynamic_cast<TempShape *>(startShape()))
		return dynamic_cast<ActionShape *>(s)
			|| dynamic_cast<DecisionShape *>(s)
			|| dynamic_cast<InitialNodeShape *>(s)
			|| dynamic_cast<FinalNodeShape *>(s)
			|| dynamic_cast<ArrowFlow *>(s)
			|| dynamic_cast<NoteShape *>(s);
	else return dynamic_cast<NoteShape *>(s);
}

NoteFlow::NoteFlow (Shape *start, Shape *end, QGraphicsItem *parent)
	: Flow(start, end, parent)
{
	updateFlow();
}

void NoteFlow::paint (QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
	updateAnchors();
	QPen pen(Qt::CustomDashLine);
	pen.setDashPattern(QVector<qreal>() << 5 << 3);
	painter->setPen(pen);
	painter->drawPath(basicShape());
	if (isSelected()) paintSelection(painter, option, widget);
}
